/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Documents;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mukesh
 */
public class AdmissionDocumentEdit extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int id = 0;
            if (request.getParameter("id").length() != 0) {
                id = Integer.parseInt(request.getParameter("id"));
            }

            Vector<Object> list = Documents.Query.viewAll();
            Vector<Object> list1 = Documents.Query.viewSpecific("ADMISSION_ID", id);
            StringBuilder stb = new StringBuilder();
            out.println("<table class='table table-striped dataTable-noheader dataTable-nofooter table-bordered'><thead>");
            out.println("<th></th>");
            out.println("<th>Document Name</th>");
            out.println("<th>Document Number</th>");
            out.println("<th>Upload Document</th>");
            out.println("</thead><tbody>");
            Iterator it = list.iterator();
            dd: while (it.hasNext()) {
                DocumentsBean db = (DocumentsBean) it.next();
                for (int i = 0; i < list1.size(); i++) {
                    StudentDocumentBean sdb = (StudentDocumentBean) list1.get(i);
                    if (db.getDocumentId() == sdb.getDocumentId()) {
                        out.println("<tr><td style='width:30px'><center><input type='checkbox' name='" + sdb.getDocumentId() + "-c' id='" + sdb.getDocumentId() + "-c' checked disabled></center></td><td><center>" + db.getDocumentName() + "</center></td>");
                        out.println("</td><td><center><input type='text' name='" + db.getDocumentId() + "-t' id='" + db.getDocumentId() + "-t' value='"+sdb.getDocumentNo()+"' class='input-square required' style='margin: 10px;' disabled></center></td>");
                        out.println("</td><td><a href='#' style='color:blue;'>Download</a></td></tr>");
                        
                        continue dd;
                    }
                }
                stb.append(db.getDocumentId() + "/");
                out.println("<tr><td style='width:30px'><center><input type='checkbox' name='" + db.getDocumentId() + "-c' id='" + db.getDocumentId() + "-c' onclick='enableDocument(this.id)'></center></td><td><center>" + db.getDocumentName() + "</center></td>");
                out.println("</td><td><center><input type='text' name='" + db.getDocumentId() + "-t' id='" + db.getDocumentId() + "-t' class='input-square required' style='margin: 10px;' disabled></center></td>");
                out.println("</td><td><input type='file'  name='" + db.getDocumentId() + "-f' id='" + db.getDocumentId() + "-f' class='input-square required' style='margin: 10px;' disabled></td></tr>");

            }

            out.println("</tbody></table>");
            out.println("<div class='form-actions' style='clear: both; border-bottom: 0px solid #e5e5e5;'>");
            out.println("<input type='hidden' id='docid' name='docid' value='" + stb.toString() + "'>");
            out.println("<input type='submit' class='btn btn-primary'>");
            out.println("</div>");
        } catch (Exception e) {
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDocumentEdit.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDocumentEdit.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
